package com.sosee.cgfw.gaodemap;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.sosee.cgfw.R;
import com.sosee.cgfw.gaodemap.util.Utils;
import com.sosee.cgfw.utils.LogUtils;

/**
 * 
 *导航界面
 * 
 * */
public class SimpleNaviActivity extends BaseActivity implements
        AMapNaviViewListener {
	//导航View
   //是否为模拟导航
	private boolean mIsEmulatorNavi = false;
	private TTSController tManager;
	//记录有哪个页面跳转而来，处理返回键
	private int mCode=-1;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_simplenavi);
		Bundle bundle = getIntent().getExtras();
		processBundle(bundle);
		init(savedInstanceState);
	
	}

	private void processBundle(Bundle bundle) {
		if (bundle != null) {
			mIsEmulatorNavi = bundle.getBoolean(Utils.ISEMULATOR, true);
			mCode=bundle.getInt(Utils.ACTIVITYINDEX);
		}
	}

	/**
	 * 初始化
	 * 
	 * @param savedInstanceState
	 */
	private void init(Bundle savedInstanceState) {
		mAMapNaviView = (AMapNaviView) findViewById(R.id.simplenavimap);
		mAMapNaviView.onCreate(savedInstanceState);
		mAMapNaviView.setAMapNaviViewListener(this);
		LogUtils.log_E("启动讯飞语音导航");
		tManager=TTSController.getInstance(this);
		tManager.init();
		tManager.startSpeaking();
		AMapNavi aMapNavi;
		mIsEmulatorNavi=false;
		if (mIsEmulatorNavi) {
			// 设置模拟速度
			 AMapNavi.getInstance(this).setEmulatorNaviSpeed(100);
			// 开启模拟导航
			AMapNavi.getInstance(this).startNavi(AMapNavi.EmulatorNaviMode);

		} else {
			LogUtils.log_E("开启实时导航====");
			//开启实时导航
			aMapNavi=AMapNavi.getInstance(this);
//			aMapNavi.setAMapNaviListener(tManager);
			aMapNavi.startNavi(AMapNavi.GPSNaviMode);

		}
	}

//-----------------------------导航界面回调事件------------------------
	/**
	 * 导航界面返回按钮监听
	 * */
	@Override
	public void onNaviCancel() {
		finish();
	}
   
	@Override
	public void onNaviSetting() {

	}

	@Override
	public void onNaviMapMode(int arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onNaviTurnClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNextRoadClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScanViewButtonClick() {
		// TODO Auto-generated method stub

	}
	/**
	 * 
	 * 返回键监听事件
	 * */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(mCode==Utils.SIMPLEROUTENAVI){
				Intent intent = new Intent(SimpleNaviActivity.this,
						SimpleNaviRouteActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
				
			}
//			else if(mCode==Utils.SIMPLEGPSNAVI){
//				Intent intent = new Intent(SimpleNaviActivity.this,
//						SimpleGPSNaviActivity.class);
//				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//				startActivity(intent);
//				finish();
//			}
			else{
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	// ------------------------------生命周期方法---------------------------


	@Override
	public void onLockMap(boolean arg0) {
		  
		// TODO Auto-generated method stub  
		
	}

	

}
